from rest_framework.views import APIView
from rest_framework.response import Response
from .models import Fuguo_share
from .getfuguodata import sync_data
# Create your views here.
class SyncDataView(APIView):
    def post(self, request):
        try:
            requestDict = request.data
        except:
            return Response({'code': 500,'msg': 'Invalid request json'})
        if not requestDict:
            return Response({'code': 500,'msg': 'Invalid request data'})
        if not requestDict.get('start_date_str'):
            return Response({'code': 500,'msg': 'Invalid start_date_str'})
        if not requestDict.get('end_date_str'):
            return Response({'code': 500,'msg': 'Invalid end_date_str'})
        
        sync_data(requestDict.get('start_date_str'), requestDict.get('end_date_str'))
        return Response({'code': 200,'msg': 'Data sync success'})
    
class FuguoEchartView(APIView):
    def post(self, request):
        try:
            requestDict = request.data
        except:
            return Response({'code': 500,'msg': 'Invalid request json'})
        if not requestDict:
            return Response({'code': 500,'msg': 'Invalid request data'})
        if not requestDict.get('start_date'):
            return Response({'code': 500,'msg': 'Invalid start_date'})
        if not requestDict.get('end_date'):
            return Response({'code': 500,'msg': 'Invalid end_date'})
        fuguo_shares = Fuguo_share.objects.filter(day__range=[requestDict.get('start_date'), requestDict.get('end_date')]).order_by('day')
        xAxis_data = []
        series_data = []
        series_bizhu_data = []
        datas_01 = []
        datas_02 = []
        datas_03 = []
        datas_04 = []
        datas_05 = []
        for share in fuguo_shares:
            if share.day.strftime('%Y-%m-%d') not in xAxis_data:
                xAxis_data.append(share.day.strftime('%Y-%m-%d'))
            if share.TRADE_TYPE == '认沽交易量':
                datas_01.append(float(share.VOLUME))
            elif share.TRADE_TYPE == '认沽持仓量':
                datas_02.append(float(share.VOLUME))
            elif share.TRADE_TYPE == '认购交易量':
                datas_03.append(float(share.VOLUME))
            elif share.TRADE_TYPE == '认购持仓量':
                datas_04.append(float(share.VOLUME))
        for i in range(0,len(datas_01)):
            datas_05.append(datas_02[i]/datas_04[i]*100)
        series_data.append({'name': '认沽交易量', 'type': 'line','stack': 'Total', 'data': datas_01})
        series_data.append({'name': '认沽持仓量', 'type': 'line','stack': 'Total','data': datas_02})
        series_data.append({'name': '认购交易量', 'type': 'line','stack': 'Total', 'data': datas_03})
        series_data.append({'name': '认购持仓量', 'type': 'line','stack': 'Total', 'data': datas_04})
        series_bizhu_data.append({'name': '认购持仓量', 'type': 'line','stack': 'Total', 'data': datas_05})
        return Response({'code': 200, 'xAxis_data': xAxis_data,'series_data': series_data,'series_bizhu_data':series_bizhu_data})